// kooDlg.cpp : implementation file
//

#include "stdafx.h"
#include "koo.h"
#include "kooDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CKooDlg dialog

CKooDlg::CKooDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CKooDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CKooDlg)	
	m_val_s1 = 0;	
	m_val_s2 = 0;
	m_val_s3 = 0;	
	m_val_s4 = 0;
	m_val_port = 0;		
	m_val_m1 = 0;	
	m_val_m2 = 0;
	m_val_m3 = 0;	
	m_val_m4 = 0;
	m_val_l1 = 0;	
	m_val_l2 = 0;
	m_val_l3 = 0;	
	m_val_l4 = 0;
	m_val_g1 = 0;	
	m_val_g2 = 0;
	m_val_g3 = 0;	
	m_val_g4 = 0;
	m_val_ss1 = 0;	
	m_val_ss2 = 0;
	m_val_ss3 = 0;	
	m_val_ss4 = 0;
	m_val_pport = 0;		
	m_val_mm1 = 0;	
	m_val_mm2 = 0;
	m_val_mm3 = 0;	
	m_val_mm4 = 0;
	m_val_ll1 = 0;	
	m_val_ll2 = 0;
	m_val_ll3 = 0;	
	m_val_ll4 = 0;
	m_val_gg1 = 0;	
	m_val_gg2 = 0;
	m_val_gg3 = 0;	
	m_val_gg4 = 0;
	m_val_posno = 0;
	m_val_pposno = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CKooDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CKooDlg)	
	DDX_Text(pDX, IDC_EDIT1, m_val_s1);	
	DDX_Text(pDX, IDC_EDIT2, m_val_s2);
	DDX_Text(pDX, IDC_EDIT3, m_val_s3);	
	DDX_Text(pDX, IDC_EDIT4, m_val_s4);
	DDX_Text(pDX, IDC_EDIT5, m_val_port);	
	DDX_Text(pDX, IDC_EDIT6, m_val_m1);
	DDX_Text(pDX, IDC_EDIT7, m_val_m2);	
	DDX_Text(pDX, IDC_EDIT8, m_val_m3);
	DDX_Text(pDX, IDC_EDIT9, m_val_m4);	
	DDX_Text(pDX, IDC_EDIT10, m_val_l1);
	DDX_Text(pDX, IDC_EDIT11, m_val_l2);	
	DDX_Text(pDX, IDC_EDIT12, m_val_l3);
	DDX_Text(pDX, IDC_EDIT13, m_val_l4);	
	DDX_Text(pDX, IDC_EDIT27, m_val_g1);
	DDX_Text(pDX, IDC_EDIT28, m_val_g2);	
	DDX_Text(pDX, IDC_EDIT29, m_val_g3);
	DDX_Text(pDX, IDC_EDIT30, m_val_g4);
	DDX_Text(pDX, IDC_EDIT14, m_val_ss1);
	DDX_Text(pDX, IDC_EDIT15, m_val_ss2);
	DDX_Text(pDX, IDC_EDIT16, m_val_ss3);
	DDX_Text(pDX, IDC_EDIT17, m_val_ss4);
	DDX_Text(pDX, IDC_EDIT18, m_val_pport);
	DDX_Text(pDX, IDC_EDIT19, m_val_mm1);
	DDX_Text(pDX, IDC_EDIT20, m_val_mm2);
	DDX_Text(pDX, IDC_EDIT21, m_val_mm3);
	DDX_Text(pDX, IDC_EDIT22, m_val_mm4);
	DDX_Text(pDX, IDC_EDIT23, m_val_ll1);
	DDX_Text(pDX, IDC_EDIT24, m_val_ll2);
	DDX_Text(pDX, IDC_EDIT25, m_val_ll3);
	DDX_Text(pDX, IDC_EDIT26, m_val_ll4);	
	DDX_Text(pDX, IDC_EDIT31, m_val_gg1);
	DDX_Text(pDX, IDC_EDIT32, m_val_gg2);	
	DDX_Text(pDX, IDC_EDIT33, m_val_gg3);
	DDX_Text(pDX, IDC_EDIT34, m_val_gg4);
	DDX_Text(pDX, IDC_EDIT35, m_val_posno);
	DDX_Text(pDX, IDC_EDIT36, m_val_pposno);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CKooDlg, CDialog)
	//{{AFX_MSG_MAP(CKooDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CKooDlg message handlers

BOOL CKooDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_val_m1 = 255;
	m_val_m2 = 255;
	m_val_m3 = 255;
	m_val_m4 = 0;
	UpdateData(false);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CKooDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CKooDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CKooDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CKooDlg::OnButton1() 
{
	FILE *fp;
	int ret = 0;
	
	UpdateData();	
	memset(&g_wvid,0,sizeof(Vid));
	g_wvid.s1=m_val_s1;
	g_wvid.s2=m_val_s2;
	g_wvid.s3=m_val_s3;
	g_wvid.s4=m_val_s4;
	g_wvid.port=m_val_port;
	g_wvid.m1=m_val_m1;
	g_wvid.m2=m_val_m2;
	g_wvid.m3=m_val_m3;
	g_wvid.m4=m_val_m4;
	g_wvid.l1=m_val_l1;
	g_wvid.l2=m_val_l2;
	g_wvid.l3=m_val_l3;
	g_wvid.l4=m_val_l4;
	g_wvid.g1=m_val_g1;
	g_wvid.g2=m_val_g2;
	g_wvid.g3=m_val_g3;
	g_wvid.g4=m_val_g4;
	g_wvid.posno=m_val_posno;
		
	if ((fp=fopen("a.bin","wb+")) != NULL)
	{
		rewind(fp);
		ret = fwrite(&g_wvid,sizeof(Vid) + 1,1,fp);
		fclose(fp);
		MessageBox("ookk","");
	}
}

void CKooDlg::OnButton2() 
{
	FILE *fp;
	int ret = 0,li_ret=0;

	UpdateData(false);
	memset(&g_rvid,0,sizeof(Vid));
	if ((fp=fopen("a.bin","rb")) == NULL)
	{
		return;
	}
	
	rewind(fp);
	ret = fread(&g_rvid,sizeof(Vid)+1,1,fp);
	fclose(fp);
	
	m_val_ss1 = g_rvid.s1;
	m_val_ss2 = g_rvid.s2;
	m_val_ss3 = g_rvid.s3;
	m_val_ss4 = g_rvid.s4;
	m_val_pport = g_rvid.port;
	m_val_mm1 = g_rvid.m1;
	m_val_mm2 = g_rvid.m2;
	m_val_mm3 = g_rvid.m3;
	m_val_mm4 = g_rvid.m4;
	m_val_ll1 = g_rvid.l1;
	m_val_ll2 = g_rvid.l2;
	m_val_ll3 = g_rvid.l3;
	m_val_ll4 = g_rvid.l4;
	m_val_gg1 = g_rvid.g1;
	m_val_gg2 = g_rvid.g2;
	m_val_gg3 = g_rvid.g3;
	m_val_gg4 = g_rvid.g4;
	m_val_pposno = g_rvid.posno;

	UpdateData(false);
}

void CKooDlg::OnButton3() 
{
	FILE *fp;
	int ret = 0,li_ret=0;

	UpdateData(false);
	memset(&g_rvid,0,sizeof(Vid));
	if ((fp=fopen("a.bin","rb")) == NULL)
	{
		return;
	}
	
	rewind(fp);
	ret = fread(&g_rvid,sizeof(Vid)+1,1,fp);
	fclose(fp);
	
	m_val_s1 = g_rvid.s1;
	m_val_s2 = g_rvid.s2;
	m_val_s3 = g_rvid.s3;
	m_val_s4 = g_rvid.s4;
	m_val_port = g_rvid.port;
	m_val_m1 = g_rvid.m1;
	m_val_m2 = g_rvid.m2;
	m_val_m3 = g_rvid.m3;
	m_val_m4 = g_rvid.m4;
	m_val_l1 = g_rvid.l1;
	m_val_l2 = g_rvid.l2;
	m_val_l3 = g_rvid.l3;
	m_val_l4 = g_rvid.l4;
	m_val_g1 = g_rvid.g1;
	m_val_g2 = g_rvid.g2;
	m_val_g3 = g_rvid.g3;
	m_val_g4 = g_rvid.g4;
	m_val_posno = g_rvid.posno;

	UpdateData(false);	
}
